<?php
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
session_name(SESSION);
session_start();
$up_num = 10;
$down_num = 5;

$url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

$myPdo = DbUtils::createPdoInst();
$inst_id = MiscUtils::getParam('inid', 0);
$comp_track_id = MiscUtils::getParam('t', 0);

if (isset($_SESSION['index' . USER])) { 
  $cond_vals = new stdClass();
  $cond_vals->c = 'comp_track_id = :v1 AND client_id = :v2';
  $cond_vals->v = array(':v1' => $comp_track_id, ':v2' => $_SESSION['index' . USER]->id);
  $entries = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'comp_entry', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
  if (count($entries) == 0) {
    $obj = new stdClass();
    $obj->track_id = MiscUtils::unique();
    $obj->date = date('Y-m-d');
    $obj->client_id = $_SESSION['index' . USER]->id;
    $obj->comp_track_id = $comp_track_id;
    $obj->address = '';
    $obj->total_votes = 0;
    $obj->is_recommended = 0;
    $obj->is_read = 0;
    DbUtils::add($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'comp_entry', $obj);
    $photos = array();
  } else {
    if ($entries[0]->is_confirmed == 1) {
      echo "
      <script>
      parent.location.href = 'http://" . URL . "view.php?t=" . $entries[0]->track_id . "';
      </script>
      ";
      return;
    } else {
      $cond_vals = new stdClass();
      $cond_vals->c = 'comp_entry_track_id = :v1';
      $cond_vals->v = array(':v1' => $entries[0]->track_id);
      $photos = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'comp_entry_pic', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    }
    
  }
} else {
  echo "
  <script>
  parent.location.href = 'http://" . URL . "index.php?i=" . CLIENT_LOGIN_ID . "&url=' + encodeURIComponent(document.referrer);
  </script>
  ";
  return;
}

?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">

  <style>
    .mybox td {
      background: #fff;
      padding: 5px 5px 0 5px;
      text-align: center;
      border: 1px solid #aaa;
    }
    .mybtn {
      background:url(images/btn3.png) no-repeat;
      width: 60px;
      height: 28px;
      border: none;
      cursor: pointer;
    }
    .mybtn:hover {
      background:url(images/btn32.png) no-repeat;
    }
    .mybtn:active {
      position:relative;
      top:1px;
    }
    .join {
      margin-left: 400px;
    }
    .join textarea {
      width: 300px;
      height: 250px;
      resize: none;
      border: 1px solid #aaa;
    }
    .mybtn2 {
      background:url(images/btn_sub.png) no-repeat;
      width: 120px;
      height: 60px;
      border: none;
      cursor: pointer;
    }
  </style>

</head>
<body>
<span id="upimg"></span>
<span id="canimg" style="display: none;"></span>
<div>
  <table class="mybox" cellspacing="5">
    <tr>
      <?php foreach ($photos as $idx => &$photo) { ?>
      <?php if ($idx != 0 & $idx % 7 == 0) { ?>
      </tr>
      <tr>
      <?php } ?>
      <td>
        <form action="<?php echo basename(__FILE__, '.php'); ?>.server.php" method="post">
          <input type="text" name="action" value="remove" style="display: none;"></input>
          <input type="text" name="id" value="<?php echo $photo->id; ?>" style="display: none;"></input>
          <input type="text" name="from" value="http://<?php echo URL . 'join.php?t=' . $comp_track_id; ?>" style="display: none;"></input>
          <img src="<?php echo $photo->path; ?>" width="72" height="72" alt=""></img><br>
          <input type="submit" value="" class="mybtn"></input>
        </form>
      </td>
       <?php } ?>
    </tr>
  </table>
</div>
<?php if (count($photos) >= $down_num) { ?>
<div>&nbsp;</div>
<div class="join">
  <form action="<?php echo basename(__FILE__, '.php'); ?>.server.php" method="post">
    <input type="text" name="action" value="update" style="display: none;"></input>
    <input type="text" name="id" value="<?php echo $entries[0]->id; ?>" style="display: none;"></input>
    <input type="text" name="url" value="http://<?php echo URL . 'view.php?t=' . $entries[0]->track_id; ?>" style="display: none;"></input>
    <img src="images/addr.png" alt=""></img><br>
    <textarea name="address"></textarea><br>
    <input type="submit" value="" class="mybtn2"></input>
  </form>
<?php } else { ?>
<img src="images/expl.png" alt=""></img><br>
<?php } ?>
</div>
<script type="text/javascript" src="../../../libs/jquery/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="../../../libs/swfupload/swfupload.js"></script>
<script type="text/javascript" src="../../../libs/swfupload/swfupload.queue.js"></script>
<script type="text/javascript" src="../../../libs/swfupload/myupload2.js"></script>
<script type="text/javascript">
  $(function(){
    <?php if (count($photos) < $up_num) { ?>
    new MyUpload('upimg', 'upload.php', function(msgs) {
      location.reload();
    }, {
      dir: '../../../',
      button_image_url: 'images/browse.png',
      button_width: 156,
      button_height: 37,
      file_queue_limit: <?php echo $up_num - count($photos); ?>,
      file_size_limit: '8MB',
      file_types: '*.jpg;*.jpeg;*.png;*.gif',
      file_types_description: 'Image Files',
      cancelBtnId: 'canimg',
      post_params: {
        comp_t: '<?php echo $comp_track_id; ?>',
        entry_t: '<?php echo $entries[0]->track_id; ?>',
        client_i: '<?php echo $_SESSION['index' . USER]->id; ?>'
      },
      debug: 0
    });
    <?php } ?>
  });


</script>
</body>
</html>
